package fi.helsinki.cs.ohtu10k.view;

import javax.swing.*;

import java.awt.Frame;
import java.awt.event.*;
import com.sun.java.layout.SpringUtilities;
import org.joda.time.DateTime;

import fi.helsinki.cs.ohtu10k.schedule.ScheduleManagerHandler;

/**
 * Class implementing new course dialog.
 * @author Hannu Kärnä
 * @author Thomas Nyman
 *
 */
@SuppressWarnings("serial")
public class AddCourseDialog extends JDialog implements ActionListener{
	
	private JPanel panel;
	private JButton buttonAddCourse, buttonCancel;
	private JTextField fieldName, fieldAbbr;
	private DateSelector selectorStartDate, selectorEndDate;
	
	/**
	 * Displays the add course dialog.
	 * @param frame Owning frame. 
	 */
	public static void showAddCourseDialog(Frame owner) {		
		new AddCourseDialog(owner);
	}
	
	/**
	 * Constructs a new AddCourseDialog.
	 * @param frame Owning frame.
	 */
	public AddCourseDialog(Frame owner){		
		super(owner, Constants.LABEL_ADD_COURSE_TITLE, true);

		panel = new JPanel();
		panel.setLayout(new SpringLayout());
		
		// Initialize interactive form components
		fieldName = new JTextField(20);
		fieldAbbr = new JTextField(20);
		selectorStartDate = new DateSelector();
		selectorEndDate = new DateSelector();
		buttonAddCourse = new JButton(Constants.LABEL_ADD_COURSE);
		buttonCancel = new JButton(Constants.LABEL_CANCEL);
		
		// Initialize static form components and add components to panel
		panel.add(new JLabel(Constants.LABEL_ADD_COURSE_NAME));
		panel.add(fieldName);
		panel.add(new JLabel(Constants.LABEL_ADD_COURSE_ABBR));
		panel.add(fieldAbbr);
		panel.add(new JLabel(Constants.LABEL_DATE_STARTING));
		panel.add(selectorStartDate);
		panel.add(new JLabel(Constants.LABEL_DATE_ENDING));
		panel.add(selectorEndDate);
		panel.add(new JPanel());
		panel.add(new JPanel());
		panel.add(buttonAddCourse);
		panel.add(buttonCancel);

		buttonAddCourse.addActionListener(this);
		buttonCancel.addActionListener(this);
		
		// Build compact layout
		SpringUtilities.makeCompactGrid(panel,
                						6, 2,	//rows, columns
                						18, 12,	//initX, initY
                						18, 12);//xPad, yPad
		
		setContentPane(panel);
		pack();
	    setLocationRelativeTo(owner);
	    setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		DateTime startDate = selectorStartDate.getDate();
		DateTime endDate = selectorEndDate.getDate();	
		
		if(e.getSource()==buttonAddCourse) {
			if (endDate.isBefore(startDate)) {
				JOptionPane.showMessageDialog(this,
											  Constants.LABEL_ERROR_ENDDATE_BEFORE_STARTDATE,
						  					  Constants.LABEL_ERROR,
						  					  JOptionPane.ERROR_MESSAGE);
			} else {	
				ScheduleManagerHandler.getScheduleManager().addCourse(startDate, endDate, 
																	  fieldName.getText(), 
																	  fieldAbbr.getText());
				setVisible(false);
			}
		} else {
			setVisible(false);
		}
	}

}
